using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CrazyDogToDo.Service.Services;
using CrazyDogToDo.Entity.Data;
using CrazyToDo.Web.Models;
using Microsoft.AspNetCore.Http;

namespace CrazyToDo.Web.Controllers
{
    public class MatterController : Controller
    {
        private readonly MatterService _matterService = new MatterService();
        public IActionResult Index()
        {
            List<Matter> matters = _matterService.GetAllMatter(HttpContext.Session.GetString("user")).ToList();
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                Content = item.Content,
                Remark = item.Remark,
                CreateTime = item.CreateTime,
                OverdueTime = item.OverdueTime,
                State = item.State
            }).ToList();
            return View("Matter", matterViewModels);
        }
        [HttpGet]
        public IActionResult GetAllMatter (int id)
        {
            _matterService.ChangeMatterState(HttpContext.Session.GetString("user"), id);
            List<Matter> matters = _matterService.GetAllMatter(HttpContext.Session.GetString("user")).ToList();
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                Content = item.Content,
                Remark = item.Remark,
                CreateTime = item.CreateTime,
                OverdueTime = item.OverdueTime,
                State = item.State
            }).ToList();
            return View(matterViewModels);
        }
        [HttpGet]
        public IActionResult DeleteMatter(int id)
        {
            _matterService.DeleteMatter(id);
            List<Matter> matters = _matterService.GetAllMatter(HttpContext.Session.GetString("user")).ToList();
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                Content = item.Content,
                Remark = item.Remark,
                CreateTime = item.CreateTime,
                OverdueTime = item.OverdueTime,
                State = item.State
            }).ToList();
            return View("GetAllMatter", matterViewModels);
        }

    }
}